package com.milotnt.service.impl;


import com.milotnt.mapper.ClassOrderMapper;
import com.milotnt.mapper.MemberMapper;

import com.milotnt.service.StatsService;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class StatsServiceImpl implements StatsService {

    private final MemberMapper memberMapper;
    private final ClassOrderMapper classOrderMapper;

    public StatsServiceImpl(MemberMapper memberMapper, ClassOrderMapper classOrderMapper) {
        this.memberMapper = memberMapper;
        this.classOrderMapper = classOrderMapper;
    }

    @Override
    public Map<String, Object> getMonthlyMemberStats() {
        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
        List<Map<String, Object>> rawData = memberMapper.countMonthlyRegistrations(currentYear);

        Map<String, Object> result = new HashMap<>();
        List<String> labels = Arrays.asList("1月", "2月", "3月", "4月", "5月", "6月", "7月");
        List<Integer> data = new ArrayList<>(Collections.nCopies(7, 0));

        for (Map<String, Object> row : rawData) {
            int month = ((Number) row.get("month")).intValue();
            int count = ((Number) row.get("count")).intValue();
            if (month >= 1 && month <= 7) {
                data.set(month - 1, count);
            }
        }

        result.put("labels", labels);
        result.put("data", data);
        return result;
    }

    @Override
    public Map<String, Object> getCourseEnrollmentStats() {
        List<Map<String, Object>> rawData = classOrderMapper.countCourseEnrollments();

        Map<String, Object> result = new HashMap<>();
        List<String> labels = new ArrayList<>();
        List<Integer> data = new ArrayList<>();

        for (Map<String, Object> row : rawData) {
            labels.add((String) row.get("courseName"));
            data.add(((Number) row.get("count")).intValue());
        }

        result.put("labels", labels);
        result.put("data", data);
        return result;
    }
}